package com.diver.flinkdemo.utils.excel;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * Excel 文档样式 工具类
 *
 * @author fengyun.guo  2017-09-01 16:07:20
 */
public class ExcelStyle {

    /**
     * 设置 Excel 工作薄 的标题样式
     *
     * @param workbook Excel工作薄对象
     * @return Excel工作薄对象的标题样式
     */
    public static HSSFCellStyle setHeadStyle(HSSFWorkbook workbook) {
        HSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        // 背景色
//        headStyle.setFillForegroundColor(HSSFColor.DARK_TEAL.index);//前景色
//        //headStyle.setFillBackgroundColor(HSSFColorPredefined.YELLOW.getIndex());
//        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        // 设置边框
//        headStyle.setBorderBottom(BorderStyle.THIN);
//        headStyle.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index);
//        headStyle.setBorderLeft(BorderStyle.THIN);
//        headStyle.setLeftBorderColor(HSSFColor.GREY_50_PERCENT.index);
//        headStyle.setBorderRight(BorderStyle.THIN);
//        headStyle.setRightBorderColor(HSSFColor.GREY_50_PERCENT.index);
//        headStyle.setBorderTop(BorderStyle.THIN);
//        headStyle.setTopBorderColor(HSSFColor.GREY_50_PERCENT.index);
        // 生成字体
        HSSFFont font = workbook.createFont();
//        font.setColor(HSSFColor.GREY_25_PERCENT.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样样式
        headStyle.setFont(font);
        return headStyle;

    }

    /**
     * 设置 Excel 工作薄 的内容样式
     *
     * @param workbook Excel工作薄对象
     * @return Excel工作薄对象的内容样式
     */
    public static HSSFCellStyle setBodyStyle(HSSFWorkbook workbook) {
        HSSFCellStyle bodyStyle = workbook.createCellStyle();
//        bodyStyle.setFillForegroundColor(HSSFColor.WHITE.index);
        bodyStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 设置边框
        bodyStyle.setBorderBottom(BorderStyle.THIN);
//        bodyStyle.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index);
        bodyStyle.setBorderLeft(BorderStyle.THIN);
//        bodyStyle.setLeftBorderColor(HSSFColor.GREY_50_PERCENT.index);
        bodyStyle.setBorderRight(BorderStyle.THIN);
//        bodyStyle.setRightBorderColor(HSSFColor.GREY_50_PERCENT.index);
        bodyStyle.setBorderTop(BorderStyle.THIN);
//        bodyStyle.setTopBorderColor(HSSFColor.GREY_50_PERCENT.index);

        bodyStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成字体
        HSSFFont font = workbook.createFont();
        font.setBold(false);
//        font.setColor(HSSFColor.GREY_80_PERCENT.index);
        font.setFontHeightInPoints((short) 11);
        // 把字体应用到当前的样样式
        bodyStyle.setFont(font);
        return bodyStyle;
    }
}
